//----------------------------------------------------------------------------
// M A R I T I M E  R E S E A R C H  I N S T I T U T E  N E T H E R L A N D S
//----------------------------------------------------------------------------
// Copyright (C) 2008 - MARIN - All rights reserved - http://www.marin.nl
//----------------------------------------------------------------------------
// Program    : mermaid
// Module     : vbm
// File       : StringUtils.cpp
//----------------------------------------------------------------------------

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <algorithm> // min+max
#include "StringUtils.h"

/// trims whitespace from both sides of string
char* StringUtils::trim(char *s)
{
    int i;
    char *ptr;

    ptr = s;
    while (isspace(*ptr))
    {
        ++ptr;
    }
    for (i = (int)strlen(s) - 1; i >= 0 && isspace(s[i]); --i)
    {
        s[i] = '\0';
    }
    return ptr;
}


/// splits string in situ on given character
int StringUtils::split(char *str, char *word[], int max, char c)
{
    int n;

    n = 0;
    do
    {
        while (*str == c) str++;
        if (!*str || n == max) return n;

        word[n++] = str++;
    while (*str && *str != c) str++;

        if (!*str) return n;

        *str++ = '\0';
    }
    while (1);
} /* split */

